package chat.bean;

import java.util.*;

import org.apache.logging.log4j.*; 

public class ChatRoomList extends ArrayList<ChatRoom> {

	private static final long serialVersionUID = -5653461501984903835L;

	/**
	 * Log4j logger
	 */
	static final Logger logger = LogManager.getLogger(ChatRoomList.class);   
	
	public ChatRoomList() { 
	}  
	
	/**
	 * 채팅방 이름에 해당하는 채팅방을 반환한다.
	 * 없으면 채팅방을 생성한 후, 리스트에 추가한 다음에 반환한다.
	 * @param chatRoomName 채팅방 이름
	 * @return
	 */
	public ChatRoom getChatRoomByName( String chatRoomName ) { 
		
		chatRoomName = chatRoomName == null ? "" : chatRoomName;
		
		chatRoomName = chatRoomName.trim();
		
		ArrayList<ChatRoom> chatRooms = this ;
		
		for( ChatRoom chatRoom : chatRooms ) {
			if( chatRoomName.equals( chatRoom.getChatRoomName() ) ) {
				return chatRoom;
			}
		}
		
		ChatRoom chatRoom = new ChatRoom();
		chatRoom.setChatRoomName( chatRoomName );
		
		chatRooms.add( chatRoom );
		
		return chatRoom;
	}
	
	/**
	 * 채팅방 이름에 해당하는 채팅방을 반환한다.
	 * 없으면 채팅방을 생성한 후, 리스트에 추가한 다음에 반환한다.
	 * @param chatRoomId 채팅방 아이디
	 * @return
	 */
	public ChatRoom getChatRoomById( int chatRoomId ) {  
		
		ArrayList<ChatRoom> chatRooms = this ;
		
		for( ChatRoom chatRoom : chatRooms ) {
			if( chatRoomId == chatRoom.getChatRoomId() ) {
				return chatRoom;
			}
		}
		
		ChatRoom chatRoom = new ChatRoom();
		chatRoom.setChatRoomId( chatRoomId );
		
		chatRooms.add( chatRoom );
		
		return chatRoom;
	}

}
